﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class LoaiXeService : IService<LoaiXe>
    {
        private readonly IEntityRepository<LoaiXe> _loaiXeRepository;
        public LoaiXeService(IEntityRepository<LoaiXe> loaiXeRepository)
        {
            _loaiXeRepository = loaiXeRepository;
        }

        public LoaiXe Create(LoaiXe t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _loaiXeRepository.InsertOnCommit(t);
                _loaiXeRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public LoaiXe Update(LoaiXe t)
        {
            LoaiXe item;
            try
            {
                item = _loaiXeRepository.Get(t.Id);
                item.Ten = t.Ten;
                item.GiaTien = t.GiaTien;
                //item.Created = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                _loaiXeRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public LoaiXe Detail(Guid id)
        {
            var item = _loaiXeRepository.Get(id);
            return item;
        }

        public LoaiXe Detail(int id)
        {
            var item = _loaiXeRepository.Get(id);
            return item;
        }
        
        public List<LoaiXe> ToList()
        {
            var item = _loaiXeRepository.GetAll().ToList();
            return item;
        }

        public void Delete(LoaiXe t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _loaiXeRepository.DeleteOnCommit(t);
                _loaiXeRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
